package com.fruit.controller.app.pay;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fruit.controller.base.BaseController;
import com.fruit.pojo.order.Order;
import com.fruit.pojo.pay.PayInfo;
import com.fruit.service.order.OrderHandel;
import com.fruit.service.pay.PayManager;
import com.fruit.util.ResultInfo;


@Controller
@RequestMapping(value = "/app/pay")
public class PayController extends BaseController {
	
	@Autowired
	private PayManager payService;
	
	@Autowired
    private OrderHandel orderHandelService;
	/**
	 * 提交订单
	 * @param pi
	 * @return
	 * @author admin
	 * @version 2017
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping
	@ResponseBody
	public Map<String, Object> confirmPay(PayInfo pi){
		Map<String, Object> resMap = new HashMap<String, Object>();
		try{
		//校验购物车产品类型
		resMap = payService.saveConfirmPay(pi);
		ResultInfo ri = (ResultInfo)resMap.get("result");
		if(ResultInfo.SUCCESS.equals(ri.getState())){
			List<Order> orderList = (List<Order>)resMap.get("orderList");
			PayInfo payInfo = (PayInfo)resMap.get("pi");
			resMap.remove("orderList");
			orderHandelService.runSaveAutoCancelOrder(orderList, payInfo);

			
		}
		}catch(Exception e){
			e.printStackTrace();
			this.logError(logger, e.getMessage());
			resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
		}
		return resMap;

	}
	
	@RequestMapping(value="/result",method = { RequestMethod.POST,RequestMethod.GET })
	@ResponseBody
	public Map<String, Object> result(PayInfo pi){
		Map<String, Object> resMap = new HashMap<String, Object>();
		
		try{
			//校验购物车产品类型
			resMap = payService.getPayInfo(pi);
			}catch(Exception e){
				e.printStackTrace();
				this.logError(logger, e.getMessage());
				resMap.put("result", this.initResult(ResultInfo.ERROR, "expection", "系统繁忙!"));
			}
			return resMap;
		
	}


}
